﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace HTF_HMI
{
    public partial class AUDIT_ITEMS_EDIT : Form
    {
        public bool isAdd = true;
        public string reportId = string.Empty;
        private AUDIT_DEVICE audit_line;

        public AUDIT_ITEMS_EDIT(AUDIT_DEVICE act_form, string id, string deviceId)
        {
            InitializeComponent();
            audit_line = act_form;
            BindPermission();
            if (!string.IsNullOrEmpty(id))
            {
                reportId = id;
                isAdd = false;             
                BindItem(reportId, deviceId);
            }
            txt_deviceid.Text = deviceId;
            txt_device.Text = DBClass.DBHelper.GetAuditDeviceByDeviceID(deviceId).FirstOrDefault().DEVICE_NAME;
            txt_device.Enabled = false;           
        }

        private void BindPermission()
        {
            var stations = DBClass.DBHelper.GetAuditStation();
            int i = 0;
            foreach (DBEntity.Models.AUDIT_STATION station in stations)
            {
                System.Windows.Forms.CheckBox cb = new System.Windows.Forms.CheckBox();
                cb.Name = "cb_" + station.STATION_ID.ToString();
                cb.Tag = station.STATION_ID;
                cb.Text = station.STATION_NAME;

                pal_per.Controls.Add(cb);
                cb.Left = 10 + (i % 3) * 120;
                cb.Top = 15 + (i / 3) * 30;

                i++;
            }
        }
    
        private void BindItem(string id,string deviceId)
        {   
            var item = DBClass.DBHelper.GetAuditItemByID(id).FirstOrDefault();
            if (item != null)
            {
                txt_device.Text = item.DEVICE_NAME.ToString();
                txt_item.Text = item.ITEMS.ToString();
                txt_standard.Text = item.STANDARD;
                txt_id.Text = item.ID.ToString();
                txt_deviceid.Text = deviceId;
                string permission = item.PERMISSION_STATION;
                string[] perArray = permission.Split('&');

                foreach (Control control in pal_per.Controls)
                {
                    if (control is System.Windows.Forms.CheckBox)
                    {
                        System.Windows.Forms.CheckBox cb = (System.Windows.Forms.CheckBox)control;
                        if (!string.IsNullOrEmpty(cb.Tag.ToString()))
                        {
                            if (perArray.Contains(cb.Tag.ToString()))
                            {
                                cb.Checked = true;
                            }
                            else
                            {
                                cb.Checked = false;
                            }
                        }
                    }
                }
            }
        }       

        private void Number_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) || e.KeyChar == (char)8 || e.KeyChar == '\b')
            {
                e.Handled = true;
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btn_save_Click(object sender, EventArgs e)
        {            
            string strDevice = txt_device.Text.Trim();
            string strItem = txt_item.Text.Trim();
            string strDeviceId = txt_deviceid.Text;
            string strStandrad = txt_standard.Text.Trim();

            StringBuilder permission = new StringBuilder();
            StringBuilder permissionIds = new StringBuilder();
            foreach (Control control in pal_per.Controls)
            {
                if (control is System.Windows.Forms.CheckBox)
                {
                    System.Windows.Forms.CheckBox cb = (System.Windows.Forms.CheckBox)control;
                    if (!string.IsNullOrEmpty(cb.Tag.ToString()))
                    {
                        if (cb.Checked)
                        {
                            permission.Append("&"+ cb.Text);
                            permissionIds.Append("&" + cb.Tag);
                        }                       
                    }
                }
            }

            if (string.IsNullOrEmpty(strDevice))
            {
                MessageBox.Show("请输入巡检点位！");
                return;
            }
            if (string.IsNullOrEmpty(strItem))
            {
                MessageBox.Show("请输入巡检项目！");
                return;
            }
            if (string.IsNullOrEmpty(strStandrad))
            {
                MessageBox.Show("请输入巡检标准！");
                return;
            }
            if (string.IsNullOrEmpty(permissionIds.ToString()))
            {
                MessageBox.Show("请选择巡检岗位！");
                return;
            }

            try
            {
                string sql = string.Empty;              
                DBEntity.Models.AUDIT_ITEMS item = new DBEntity.Models.AUDIT_ITEMS();

                item.DEVICE_ID = txt_deviceid.Text;
                item.DEVICE_NAME = txt_device.Text;
                item.ITEMS = strItem;
                item.STANDARD = strStandrad;
                item.PERMISSION = permission.ToString().TrimStart('&');
                if (permissionIds.Length > 0)
                {
                    item.PERMISSION_STATION = permissionIds.ToString() + "&";
                }

                if (!isAdd)
                {
                    item.ID = Int32.Parse(txt_id.Text);                   
                    bool result = DBClass.DBHelper.UpdateAuditItem(item);
                    if (result )
                    {    
                        MessageBox.Show("保存成功！");
                        this.DialogResult = DialogResult.OK;
                        //audit_line.btn_query_Click(null, null);
                        audit_line.gridView3_SelectionChanged(null,null);
                    }
                }
                else
                {                   
                    long id = DBClass.DBHelper.InsertAuditItem(item);
                    if (id > 0)
                    {     
                        MessageBox.Show("保存成功！");
                        this.DialogResult = DialogResult.OK;
                        //audit_line.btn_query_Click(null, null);
                        audit_line.gridView3_SelectionChanged(null, null);
                    }                            
                }               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                DBClass.Log.InsertLog("保存巡检项目失败！"+ex.Message);  
            }
        }
    }
}
